#pragma once

#include "TimeEvent.h"

#include "TimeExpression.h"

namespace uml {

const bool TimeEvent::isRelative_default_ = false;
TimeEvent::TimeEvent(const QString& umlType)
    : Event(umlType) {}

void TimeEvent::fromJson(const QJsonObject& json, QStringList& errorList) {
    if (json.isEmpty()) {
        return;
    }

    Event::fromJson(json, errorList);
    isRelative_ = json.value("isRelative").toBool(isRelative_default_);
    when_ = JsonSerializable::create<TimeExpression>(json.value("when"), errorList);

}

void TimeEvent::toJson(QJsonObject& json) {
    Event::toJson(json);
    if (isRelative_ != isRelative_default_) {
        json.insert("isRelative", isRelative_);
    }
    if (when_) {
        QJsonObject when_json;
        when_->toJson(when_json);
        json.insert("when", when_json);
    }
}

/// <summary>
/// isRelative getter
/// Specifies whether the TimeEvent is specified as an absolute or relative time.
/// </summary>
bool TimeEvent::isRelative() const {
    return isRelative_;
}

/// <summary>
/// isRelative setter
/// Specifies whether the TimeEvent is specified as an absolute or relative time.
/// </summary>
void TimeEvent::setIsRelative(bool value) {
    isRelative_ = value;
}

/// <summary>
/// when getter
/// Specifies the time of the TimeEvent.
/// </summary>
shared_ptr<TimeExpression> TimeEvent::when() const {
    return when_;
}

/// <summary>
/// when setter
/// Specifies the time of the TimeEvent.
/// </summary>
void TimeEvent::setWhen(shared_ptr<TimeExpression> value) {
    when_ = value;
}

} // namespace uml
